﻿#include "PlatformProperty.h"

MAPPING_VALUE(sdb::AvailabilitySts, PropertyStatus, PropertyStatus::NotAvailable)
    MAPPING_ITEM(sdb::AvailabilitySts::Active, PropertyStatus::Active)
    MAPPING_ITEM(sdb::AvailabilitySts::NotActive, PropertyStatus::NotActive)
    MAPPING_ITEM(sdb::AvailabilitySts::Error, PropertyStatus::Error)
    MAPPING_ITEM(sdb::AvailabilitySts::NotAvailable, PropertyStatus::NotAvailable)
MAPPING_END

float temMinCurrent(int unit) {
    if (unit == toInt(sdb::AmbTIndcdUnit::Fahrenheit)) {
        return 59.0f;
    } else {
        return 15.5f;
    }
}

float temMaxCurrent(int unit) {
    if (unit == toInt(sdb::AmbTIndcdUnit::Fahrenheit)) {
        return 85.0f;
    } else {
        return 28.5f;
    }
}

float valueBySignalsCurrent(int areaId, float last ,int unint) {
    int data = 0;
    if (areaId == static_cast<int32_t>(VehicleAreaSeat::ROW_1_LEFT)) {
        data = SIGNAL.getPA_CL_LeftTemp().data;
    } else if (areaId == static_cast<int32_t>(VehicleAreaSeat::ROW_1_RIGHT)) {
        data = SIGNAL.getPA_CL_RightTemp().data;
    } else if (areaId == static_cast<int32_t>(VehicleAreaSeat::ROW_2_LEFT)) {
        data = SIGNAL.getPA_CL_SecLeftTemp().data;
    } else if (areaId == static_cast<int32_t>(VehicleAreaSeat::ROW_2_RIGHT)) {
        data = SIGNAL.getPA_CL_SecRightTemp().data;
    } else if (areaId == static_cast<int32_t>((VehicleAreaSeat::ROW_1_LEFT | VehicleAreaSeat::ROW_2_LEFT |
                          VehicleAreaSeat::ROW_2_CENTER))) {
        data = SIGNAL.getPA_CL_LeftTemp().data;
    } else if (areaId == static_cast<int32_t>((VehicleAreaSeat::ROW_1_RIGHT | VehicleAreaSeat::ROW_2_RIGHT))) {
        data = SIGNAL.getPA_CL_RightTemp().data;
    }
    float temp;
    if (unint == toInt(VehicleUnit::FAHRENHEIT)) {
        temp = data + 58;
    } else {
        temp = data * 0.5 + 15;
    }
    VHALOG.d("HVAC_TEMPERATURE_SET unint is : %d, value changed to: %d ", unint, data);
    return temp;
}



SYSTEM_READ_PROPERTY(HVAC_TEMPERATURE_CURRENT, float) {
    builder.area(VehicleAreaSeat::ROW_1_LEFT)
            .range(temMinCurrent(getPropertyValue<int32_t>(
                    static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                    static_cast<int32_t>(VehicleArea::GLOBAL))),
                   temMaxCurrent(getPropertyValue<int32_t>(
                           static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                           static_cast<int32_t>(VehicleArea::GLOBAL))))
            .initValue([this]() {
                int unint = getPropertyValue<int32_t>(
                    static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                    static_cast<int32_t>(VehicleArea::GLOBAL));
                if (unint == toInt(VehicleUnit::FAHRENHEIT)) {
                    return 72.0f;
                } else {
                    return 22.0f;
                }
            })
            .withStatusBySignals([]([[maybe_unused]] const PropertyStatus &lastStatus) {
                sdb::AvailabilitySts availabilitySts = sdb::AvailabilitySts(SIGNAL.getPA_CL_LeftTemp().availability);
                PropertyStatus status = mappingValue(availabilitySts, PropertyStatus::NotAvailable);
                VHALOG.d("HVAC_TEMPERATURE_CURRENT row_1_left status changed to: %d ", status);
                return status;
            }, PASignal::PA_CL_LeftTemp)
            .withValueBySignals([this]([[maybe_unused]] const float &last) {
                int unint = getPropertyValue<int32_t>(static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                                          static_cast<int32_t>(VehicleArea::GLOBAL));
                return valueBySignalsCurrent(static_cast<int32_t>(VehicleAreaSeat::ROW_1_LEFT), last, unint);
            }, PASignal::PA_CL_LeftTemp)
            .area(VehicleAreaSeat::ROW_1_RIGHT)
            .range(temMinCurrent(getPropertyValue<int32_t>(
                    static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                    static_cast<int32_t>(VehicleArea::GLOBAL))),
                   temMaxCurrent(getPropertyValue<int32_t>(
                           static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                           static_cast<int32_t>(VehicleArea::GLOBAL))))
            .initValue([this]() {
                int unint = getPropertyValue<int32_t>(
                    static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                    static_cast<int32_t>(VehicleArea::GLOBAL));
                if (unint == toInt(VehicleUnit::FAHRENHEIT)) {
                    return 72.0f;
                } else {
                    return 22.0f;
                }
            })
            .withStatusBySignals([]([[maybe_unused]] const PropertyStatus &lastStatus) {
                sdb::AvailabilitySts availabilitySts = sdb::AvailabilitySts(SIGNAL.getPA_CL_RightTemp().availability);
                PropertyStatus status = mappingValue(availabilitySts, PropertyStatus::NotAvailable);
                VHALOG.d("HVAC_TEMPERATURE_CURRENT row_1_right status changed to: %d ", status);
                return status;
            }, PASignal::PA_CL_RightTemp)
            .withValueBySignals([this]([[maybe_unused]] const float &last) {
                 int unint = getPropertyValue<int32_t>(static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                                          static_cast<int32_t>(VehicleArea::GLOBAL));
                 return valueBySignalsCurrent(static_cast<int32_t>(VehicleAreaSeat::ROW_1_RIGHT), last, unint);
            }, PASignal::PA_CL_RightTemp)
            .area(VehicleAreaSeat::ROW_2_LEFT)
            .range(temMinCurrent(getPropertyValue<int32_t>(
                    static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                    static_cast<int32_t>(VehicleArea::GLOBAL))),
                   temMaxCurrent(getPropertyValue<int32_t>(
                           static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                           static_cast<int32_t>(VehicleArea::GLOBAL))))
            .initValue([this]() {
                int unint = getPropertyValue<int32_t>(
                    static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                    static_cast<int32_t>(VehicleArea::GLOBAL));
                if (unint == toInt(VehicleUnit::FAHRENHEIT)) {
                    return 72.0f;
                } else {
                    return 22.0f;
                }
            })
            .withStatusBySignals([]([[maybe_unused]] const PropertyStatus &lastStatus) {
                sdb::AvailabilitySts availabilitySts = sdb::AvailabilitySts(SIGNAL.getPA_CL_SecLeftTemp().availability);
                PropertyStatus status = mappingValue(availabilitySts, PropertyStatus::NotAvailable);
                VHALOG.d("HVAC_TEMPERATURE_CURRENT row_2_left status changed to: %d ", status);
                return status;
            }, PASignal::PA_CL_SecLeftTemp)
            .withValueBySignals([this]([[maybe_unused]] const float &last) {
                int unint = getPropertyValue<int32_t>(static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                                          static_cast<int32_t>(VehicleArea::GLOBAL));
                return valueBySignalsCurrent(static_cast<int32_t>(VehicleAreaSeat::ROW_2_LEFT), last, unint);
            }, PASignal::PA_CL_SecLeftTemp)
            .area(VehicleAreaSeat::ROW_2_RIGHT)
            .range(temMinCurrent(getPropertyValue<int32_t>(
                    static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                    static_cast<int32_t>(VehicleArea::GLOBAL))),
                   temMaxCurrent(getPropertyValue<int32_t>(
                           static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                           static_cast<int32_t>(VehicleArea::GLOBAL))))
            .initValue([this]() {
                int unint = getPropertyValue<int32_t>(
                    static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                    static_cast<int32_t>(VehicleArea::GLOBAL));
                if (unint == toInt(VehicleUnit::FAHRENHEIT)) {
                    return 72.0f;
                } else {
                    return 22.0f;
                }
            })
            .withStatusBySignals([]([[maybe_unused]] const PropertyStatus &lastStatus) {
                sdb::AvailabilitySts availabilitySts = sdb::AvailabilitySts(
                        SIGNAL.getPA_CL_SecRightTemp().availability);
                PropertyStatus status = mappingValue(availabilitySts, PropertyStatus::NotAvailable);
                VHALOG.d("HVAC_TEMPERATURE_CURRENT row_2_right status changed to: %d ", status);
                return status;
            }, PASignal::PA_CL_SecRightTemp)
            .withValueBySignals([this]([[maybe_unused]] const float &last) {
                int unint = getPropertyValue<int32_t>(static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                                          static_cast<int32_t>(VehicleArea::GLOBAL));
                return valueBySignalsCurrent(static_cast<int32_t>(VehicleAreaSeat::ROW_2_RIGHT), last, unint);
            }, PASignal::PA_CL_SecRightTemp)
            .area(VehicleAreaSeat::ROW_1_LEFT|VehicleAreaSeat::ROW_2_LEFT|VehicleAreaSeat::ROW_2_CENTER)
            .range(temMinCurrent(getPropertyValue<int32_t>(
                    static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                    static_cast<int32_t>(VehicleArea::GLOBAL))),
                   temMaxCurrent(getPropertyValue<int32_t>(
                           static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                           static_cast<int32_t>(VehicleArea::GLOBAL))))
            .initValue([this]() {
                int unint = getPropertyValue<int32_t>(
                    static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                    static_cast<int32_t>(VehicleArea::GLOBAL));
                if (unint == toInt(VehicleUnit::FAHRENHEIT)) {
                    return 72.0f;
                } else {
                    return 22.0f;
                }
            })
            .withStatusBySignals([]([[maybe_unused]] const PropertyStatus &lastStatus) {
                sdb::AvailabilitySts availabilitySts = sdb::AvailabilitySts(SIGNAL.getPA_CL_LeftTemp().availability);
                PropertyStatus status = mappingValue(availabilitySts, PropertyStatus::NotAvailable);
                VHALOG.d("HVAC_TEMPERATURE_CURRENT left status changed to: %d ", status);
                return status;
            }, PASignal::PA_CL_LeftTemp)
            .withValueBySignals([this]([[maybe_unused]] const float &last) {
                int unint = getPropertyValue<int32_t>(static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                                          static_cast<int32_t>(VehicleArea::GLOBAL));
                return valueBySignalsCurrent(static_cast<int32_t>(VehicleAreaSeat::ROW_1_LEFT | VehicleAreaSeat::ROW_2_LEFT |
                                          VehicleAreaSeat::ROW_2_CENTER),
                                      last, unint);
            }, PASignal::PA_CL_LeftTemp)
            .area(VehicleAreaSeat::ROW_1_RIGHT|VehicleAreaSeat::ROW_2_RIGHT)
            .range(temMinCurrent(getPropertyValue<int32_t>(
                    static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                    static_cast<int32_t>(VehicleArea::GLOBAL))),
                   temMaxCurrent(getPropertyValue<int32_t>(
                           static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                           static_cast<int32_t>(VehicleArea::GLOBAL))))
            .initValue([this]() {
                int unint = getPropertyValue<int32_t>(
                    static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                    static_cast<int32_t>(VehicleArea::GLOBAL));
                if (unint == toInt(VehicleUnit::FAHRENHEIT)) {
                    return 72.0f;
                } else {
                    return 22.0f;
                }
            })
            .withStatusBySignals([]([[maybe_unused]] const PropertyStatus &lastStatus) {
                sdb::AvailabilitySts availabilitySts = sdb::AvailabilitySts(SIGNAL.getPA_CL_RightTemp().availability);
                PropertyStatus status = mappingValue(availabilitySts, PropertyStatus::NotAvailable);
                VHALOG.d("HVAC_TEMPERATURE_CURRENT right status changed to: %d ", status);
                return status;
            }, PASignal::PA_CL_RightTemp)
            .withValueBySignals([this]([[maybe_unused]] const float &last) {
                int unint = getPropertyValue<int32_t>(static_cast<int32_t>(VehicleProperty::HVAC_TEMPERATURE_DISPLAY_UNITS),
                                          static_cast<int32_t>(VehicleArea::GLOBAL));
                return valueBySignalsCurrent(static_cast<int32_t>(VehicleAreaSeat::ROW_1_RIGHT | VehicleAreaSeat::ROW_2_RIGHT),
                                      last, unint);
            }, PASignal::PA_CL_RightTemp);

}
